package com.example.demo.controller;

import com.example.demo.request.BookRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.springframework.web.bind.annotation.*;

/**
 * 参数校验
 */
@RestController
@RequestMapping("/validate")
public class ValidateController {
    // 路径参数校验
    @PostMapping("/param/{id}")
    public String validateParam(@Valid @Size(min = 5, message = "id长度至少为5") @PathVariable("id") String id) {
        return "ID: " + id;
    }

    // 查询参数校验
    @PostMapping("/query")
    public String validateQuery(@Valid @NotBlank(message = "名称name不能为空") @RequestParam("name") String name) {
        return "Name: " + name;
    }

    // request校验
    @PostMapping("/request")
    public String validateRequest(@Valid @NotNull @RequestBody BookRequest book) {
        return "Book name: " + book.getName() + ", Author: " + book.getAuthor();
    }
}
